package com.fan.teamer.model.dao;

import java.util.List;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.teamer.model.domain.Attendance;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;

@Named("attendanceDao")
public class AttendanceDaoImpl implements AttendanceDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Attendance getAttendance(int id) {
		Attendance attendance = (Attendance) sessionFactory.getCurrentSession()
				.get(Attendance.class, id);
		return attendance;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Attendance> getAllAttendances() {

		return sessionFactory.getCurrentSession()
				.createCriteria(Attendance.class).list();
	}

	@Override
	public int getSize() {
		return getAllAttendances().size();
	}

	@Override
	public void addAttendance(Attendance attendance) {
		sessionFactory.getCurrentSession().save(attendance);

	}

	@Override
	public void updateStock(Attendance attendance) {
		sessionFactory.getCurrentSession().update(attendance);

	}

	@Override
	public void deleteAttendance(Attendance attendance) {
		sessionFactory.getCurrentSession().delete(attendance);
	}

	@Override
	public boolean hasAttendance(int id) {
		if (getAttendance(id) == null)
			return false;
		else
			return true;
	}

	@Override
	public List<String> getColumnNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Attendance> getMemberAttendance(Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Attendance.class);
		criteria.add(Restrictions.eq("memberEmail", member.getEmail()));
		List<Attendance> list = (List<Attendance>) criteria.list();
		return list;
	}

	

	@SuppressWarnings("unchecked")
	@Override
	public List<Attendance> getAttendancesByEvent(Event event) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Attendance.class);
		criteria.add(Restrictions.eq("eventId", event.getId()));
		return  (List<Attendance>) criteria.list();
	}

	@Override
	public Attendance getAttendance(Event event, Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Attendance.class);
		criteria.add(Restrictions.eq("eventId", event.getId()));
		criteria.add(Restrictions.eq("memberEmail", member.getEmail()));
		if(criteria.list() == null || criteria.list().size() == 0)
		{
			return null;
		}
		else
		{
			Attendance attendance = (Attendance)criteria.list().get(0);
			return attendance;
		}
	}

}
